package com.qxy.practice.concurrent;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author : 戚欣扬
 * @Description :My Dead Lock loop demo
 */
public class DeadLock {

    private int num = 5;
    private Lock lock1 = new ReentrantLock();
    private Lock lock2 = new ReentrantLock();
    private void resource1 () throws InterruptedException {
        lock1.lock();
        System.out.println(Thread.currentThread().getName()+"获得锁1，即将去拿锁2");
        Thread.sleep(2000);
        lock2.lock(); // this.yourNum


    }

    private void resource2() throws InterruptedException {
        lock2.lock();
        System.out.println(Thread.currentThread().getName()+"获得锁2，即将去拿锁1");
        Thread.sleep(2000);
        lock1.lock(); // this.myNum

    }

    public static void main(String[] args) {
        DeadLock deadLock = new DeadLock();
        new Thread(()->{
            try {
                deadLock.resource1();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        },"a").start();

        new Thread(()->{
            try {
                deadLock.resource2();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        },"b").start();

    }

}


